package com.example.statistics.ui.customer

import android.os.Bundle
import android.view.View
import com.example.statistics.R
import com.example.statistics.bean.Customer
import com.example.statistics.common.BaseActivity
import com.example.statistics.ex.bottomSingleDialog
import com.example.statistics.ex.httpPost
import com.example.statistics.net.UrlConfig
import com.example.statistics.utils.Constants
import com.example.statistics.utils.activityFinish
import com.jhj.decodelibrary.CharacterUtil
import com.jhj.httplibrary.model.HttpParams
import com.example.statistics.ex.afterTextChangedListener
import com.example.statistics.ex.contentText
import kotlinx.android.synthetic.main.activity_customer_edit.*
import org.jetbrains.anko.sdk27.coroutines.onClick

class CustomerDetailActivity : BaseActivity() {
    override val title: String
        get() = "联系人"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_customer_edit)
        val data = intent.getSerializableExtra(Constants.DATA) as Customer?
        if (data != null) {
            et_name.contentText = data.name.orEmpty()
            et_phone1.contentText = data.phoneOne.orEmpty()
            et_phone2.contentText = data.phoneTwo.orEmpty()
            et_phone3.contentText = data.phoneThree.orEmpty()
            et_company.contentText = data.company
            tv_type.contentText = data.type
            et_attribute.contentText = data.attribute
            et_product.contentText = data.product
            et_city.contentText = data.city
            et_market.contentText = data.remark
            et_address.contentText = data.address
        }

        tv_type.onClick {
            bottomSingleDialog(list = listOf("猪", "牛", "虾尾", "串", "龙虾", "火锅", "巴沙鱼")) { alertFragment, s ->
                tv_type.text = s
            }
        }
        et_attribute.onClick {
            bottomSingleDialog(list = listOf("冻品批发", "加工厂", "冷鲜肉", "熟食", "干调", "港口")) { alertFragment, s ->
                et_attribute.text = s
            }
        }
        et_phone1.afterTextChangedListener { editable, _ ->
            if (editable.toString().isNotEmpty()) {
                et_phone2.visibility = View.VISIBLE
            }
        }
        et_phone2.afterTextChangedListener { editable, _ ->
            if (editable.toString().isNotEmpty()) {
                et_phone3.visibility = View.VISIBLE
            }
        }

        var isFriend = 0
        switch_weixin.setOnCheckedChangeListener { _, b ->
            isFriend = if (b) 1 else 0
        }

        topBarRightText("保存") {

            val params = HttpParams()
            val spelling = CharacterUtil.getInstance(this).getAllSpelling(et_name?.contentText.orEmpty())
            params.put(
                    "ID" to data?.ID,
                    "name" to et_name.text?.toString(),
                    "nameSpelling" to spelling,
                    "phoneOne" to et_phone1?.text.toString(),
                    "phoneTwo" to et_phone2?.text.toString(),
                    "phoneThree" to et_phone3?.text.toString(),
                    "company" to et_company.text?.toString(),
                    "type" to tv_type.text?.toString(),
                    "attribute" to et_attribute.text?.toString(),
                    "product" to et_product.text?.toString(),
                    "city" to et_city.text?.toString(),
                    "market" to et_market.text?.toString(),
                    "address" to et_address.text?.toString(),
                    "remark" to et_remark.text?.toString(),
                    "weiXin" to isFriend
            )
            val url = if (data == null) UrlConfig.CUSTOMER_ADD else UrlConfig.CUSTOMER_UPDATE
            httpPost<Customer>(url, config = {
                isShowDialog = true
                isSuccessToast = true
                httpParams = params
            }) {
                activityFinish()
            }
        }
    }


}